<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="最近 IDEA 2020最后一个版本发布了open in new window ，已经内置了 Lombok 插件，SpringBoot 2.1.x 之后的版本也在 Starter 中内置了 Lombok 依赖。为什么他们都要支持 Lombok 呢？今天我来讲讲 Lombok 的使用，看看它有何神奇之处！  #Lombok简介Lombok是一款Java代码功能增强库，在Github上已有9.8k+">
<meta property="og:type" content="article">
<meta property="og:title" content="Lombok有啥牛皮的？SpringBoot和IDEA官方都要支持它！">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/763a7c049cf8.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="最近 IDEA 2020最后一个版本发布了open in new window ，已经内置了 Lombok 插件，SpringBoot 2.1.x 之后的版本也在 Starter 中内置了 Lombok 依赖。为什么他们都要支持 Lombok 呢？今天我来讲讲 Lombok 的使用，看看它有何神奇之处！  #Lombok简介Lombok是一款Java代码功能增强库，在Github上已有9.8k+">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:29:21.000Z">
<meta property="article:modified_time" content="2024-02-02T14:47:31.385Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/763a7c049cf8.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 22:47:31'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">Lombok有啥牛皮的？SpringBoot和IDEA官方都要支持它！</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:29:21.000Z" title="发表于 2024-01-21 17:29:21">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T14:47:31.385Z" title="更新于 2024-02-02 22:47:31">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/%E5%8F%82%E8%80%83%E7%AF%87/">参考篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>21分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Lombok有啥牛皮的？SpringBoot和IDEA官方都要支持它！"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>最近 <a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/4eo3hRwItD5ZoFxQKTBpCQ">IDEA 2020最后一个版本发布了open in new window</a> ，已经内置了 Lombok 插件，SpringBoot 2.1.x 之后的版本也在 Starter 中内置了 Lombok 依赖。为什么他们都要支持 Lombok 呢？今天我来讲讲 Lombok 的使用，看看它有何神奇之处！</p>
</blockquote>
<h2 id="Lombok简介"><a href="#Lombok简介" class="headerlink" title="#Lombok简介"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#lombok%E7%AE%80%E4%BB%8B">#</a>Lombok简介</h2><p>Lombok是一款Java代码功能增强库，在Github上已有9.8k+Star。它会自动集成到你的编辑器和构建工具中，从而使你的Java代码更加生动有趣。通过Lombok的注解，你可以不用再写getter、setter、equals等方法，Lombok将在编译时为你自动生成。</p>
<h2 id="Lombok集成"><a href="#Lombok集成" class="headerlink" title="#Lombok集成"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#lombok%E9%9B%86%E6%88%90">#</a>Lombok集成</h2><p>首先我们需要在IDEA中安装好Lombok插件，如果你使用的是最新版IDEA 2020.3，则Lombok插件已经内置，无需安装。</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:47:26_lombok_start_01-7be71d0a.png"></p>
<p>之后在项目的pom.xml文件中添加Lombok依赖，SpringBoot 2.1.x版本后无需指定Lombok版本，SpringBoot在<code>spring-boot-dependencies</code>中已经内置。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--lombok依赖--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;lombok&lt;/artifactId&gt;</span><br><span class="line">    &lt;optional&gt;true&lt;/optional&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h2 id="Lombok使用"><a href="#Lombok使用" class="headerlink" title="#Lombok使用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#lombok%E4%BD%BF%E7%94%A8">#</a>Lombok使用</h2><blockquote>
<p>Lombok中有很多注解，这些注解使得我们可以更加方便的编写Java代码，下面介绍下这些注解的使用。</p>
</blockquote>
<h3 id="val"><a href="#val" class="headerlink" title="#val"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#val">#</a>val</h3><p>使用val注解可以取代任意类型作为局部变量，这样我们就不用写复杂的ArrayList和Map.Entry类型了，具体例子如下。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/16.</span><br><span class="line"> */</span><br><span class="line">public class ValExample &#123;</span><br><span class="line"></span><br><span class="line">    public static void example() &#123;</span><br><span class="line">        //val代替ArrayList&lt;String&gt;和String类型</span><br><span class="line">        val example = new ArrayList&lt;String&gt;();</span><br><span class="line">        example.add(&quot;Hello World!&quot;);</span><br><span class="line">        val foo = example.get(0);</span><br><span class="line">        System.out.println(foo.toLowerCase());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void example2() &#123;</span><br><span class="line">        //val代替Map.Entry&lt;Integer,String&gt;类型</span><br><span class="line">        val map = new HashMap&lt;Integer, String&gt;();</span><br><span class="line">        map.put(0, &quot;zero&quot;);</span><br><span class="line">        map.put(5, &quot;five&quot;);</span><br><span class="line">        for (val entry : map.entrySet()) &#123;</span><br><span class="line">            System.out.printf(&quot;%d: %s\n&quot;, entry.getKey(), entry.getValue());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        example();</span><br><span class="line">        example2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当我们使用了val注解后，Lombok会从局部变量的初始化表达式推断出具体类型，编译后会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">public class ValExample &#123;</span><br><span class="line">    public ValExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void example() &#123;</span><br><span class="line">        ArrayList&lt;String&gt; example = new ArrayList();</span><br><span class="line">        example.add(&quot;Hello World!&quot;);</span><br><span class="line">        String foo = (String)example.get(0);</span><br><span class="line">        System.out.println(foo.toLowerCase());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void example2() &#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = new HashMap();</span><br><span class="line">        map.put(0, &quot;zero&quot;);</span><br><span class="line">        map.put(5, &quot;five&quot;);</span><br><span class="line">        Iterator var1 = map.entrySet().iterator();</span><br><span class="line"></span><br><span class="line">        while(var1.hasNext()) &#123;</span><br><span class="line">            Entry&lt;Integer, String&gt; entry = (Entry)var1.next();</span><br><span class="line">            System.out.printf(&quot;%d: %s\n&quot;, entry.getKey(), entry.getValue());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="NonNull"><a href="#NonNull" class="headerlink" title="#@NonNull"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#nonnull">#</a>@NonNull</h3><p>在方法上使用@NonNull注解可以做非空判断，如果传入空值的话会直接抛出NullPointerException。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/16.</span><br><span class="line"> */</span><br><span class="line">public class NonNullExample &#123;</span><br><span class="line">    private String name;</span><br><span class="line">    public NonNullExample(@NonNull String name)&#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        new NonNullExample(&quot;test&quot;);</span><br><span class="line">        //会抛出NullPointerException</span><br><span class="line">        new NonNullExample(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后会在构造器中添加非空判断，具体代码如下。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public class NonNullExample &#123;</span><br><span class="line">    private String name;</span><br><span class="line"></span><br><span class="line">    public NonNullExample(@NonNull String name) &#123;</span><br><span class="line">        if (name == null) &#123;</span><br><span class="line">            throw new NullPointerException(&quot;name is marked non-null but is null&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.name = name;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        new NonNullExample(&quot;test&quot;);</span><br><span class="line">        new NonNullExample((String)null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Cleanup"><a href="#Cleanup" class="headerlink" title="#@Cleanup"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#cleanup">#</a>@Cleanup</h3><p>当我们在Java中使用资源时，不可避免地需要在使用后关闭资源。使用@Cleanup注解可以自动关闭资源。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/16.</span><br><span class="line"> */</span><br><span class="line">public class CleanupExample &#123;</span><br><span class="line">    public static void main(String[] args) throws IOException &#123;</span><br><span class="line">        String inStr = &quot;Hello World!&quot;;</span><br><span class="line">        //使用输入输出流自动关闭，无需编写try catch和调用close()方法</span><br><span class="line">        @Cleanup ByteArrayInputStream in = new ByteArrayInputStream(inStr.getBytes(&quot;UTF-8&quot;));</span><br><span class="line">        @Cleanup ByteArrayOutputStream out = new ByteArrayOutputStream();</span><br><span class="line">        byte[] b = new byte[1024];</span><br><span class="line">        while (true) &#123;</span><br><span class="line">            int r = in.read(b);</span><br><span class="line">            if (r == -1) break;</span><br><span class="line">            out.write(b, 0, r);</span><br><span class="line">        &#125;</span><br><span class="line">        String outStr = out.toString(&quot;UTF-8&quot;);</span><br><span class="line">        System.out.println(outStr);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">public class CleanupExample &#123;</span><br><span class="line">    public CleanupExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) throws IOException &#123;</span><br><span class="line">        String inStr = &quot;Hello World!&quot;;</span><br><span class="line">        ByteArrayInputStream in = new ByteArrayInputStream(inStr.getBytes(&quot;UTF-8&quot;));</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            ByteArrayOutputStream out = new ByteArrayOutputStream();</span><br><span class="line"></span><br><span class="line">            try &#123;</span><br><span class="line">                byte[] b = new byte[1024];</span><br><span class="line"></span><br><span class="line">                while(true) &#123;</span><br><span class="line">                    int r = in.read(b);</span><br><span class="line">                    if (r == -1) &#123;</span><br><span class="line">                        String outStr = out.toString(&quot;UTF-8&quot;);</span><br><span class="line">                        System.out.println(outStr);</span><br><span class="line">                        return;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    out.write(b, 0, r);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; finally &#123;</span><br><span class="line">                if (Collections.singletonList(out).get(0) != null) &#123;</span><br><span class="line">                    out.close();</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            if (Collections.singletonList(in).get(0) != null) &#123;</span><br><span class="line">                in.close();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Getter-Setter"><a href="#Getter-Setter" class="headerlink" title="#@Getter&#x2F;@Setter"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#getter-setter">#</a>@Getter&#x2F;@Setter</h3><p>有了@Getter&#x2F;@Setter注解，我们再也不用编写getter&#x2F;setter方法了。试想下之前即使我们使用IDEA自动生成getter&#x2F;setter方法，如果类属性的类型和名称改了，又要重新生成getter&#x2F;setter方法也是一件很麻烦的事情。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">public class GetterSetterExample &#123;</span><br><span class="line">    @Getter</span><br><span class="line">    @Setter</span><br><span class="line">    private String name;</span><br><span class="line">    @Getter</span><br><span class="line">    @Setter(AccessLevel.PROTECTED)</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        GetterSetterExample example = new GetterSetterExample();</span><br><span class="line">        example.setName(&quot;test&quot;);</span><br><span class="line">        example.setAge(20);</span><br><span class="line">        System.out.printf(&quot;name:%s age:%d&quot;,example.getName(),example.getAge());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">public class GetterSetterExample &#123;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public GetterSetterExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return this.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setName(final String name) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Integer getAge() &#123;</span><br><span class="line">        return this.age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected void setAge(final Integer age) &#123;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="ToString"><a href="#ToString" class="headerlink" title="#@ToString"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#tostring">#</a>@ToString</h3><p>把所有类属性都编写到toString方法中方便打印日志，是一件多么枯燥无味的事情。使用@ToString注解可以自动生成toString方法，默认会包含所有类属性，使用@ToString.Exclude注解可以排除属性的生成。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@ToString</span><br><span class="line">public class ToStringExample &#123;</span><br><span class="line">    @ToString.Exclude</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line">    public ToStringExample(Long id,String name,Integer age)&#123;</span><br><span class="line">        this.id =id;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        ToStringExample example = new ToStringExample(1L,&quot;test&quot;,20);</span><br><span class="line">        //自动实现toString方法，输出ToStringExample(name=test, age=20)</span><br><span class="line">        System.out.println(example);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public class ToStringExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public ToStringExample(Long id, String name, Integer age) &#123;</span><br><span class="line">        this.id = id;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String toString() &#123;</span><br><span class="line">        return &quot;ToStringExample(name=&quot; + this.name + &quot;, age=&quot; + this.age + &quot;)&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="EqualsAndHashCode"><a href="#EqualsAndHashCode" class="headerlink" title="#@EqualsAndHashCode"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#equalsandhashcode">#</a>@EqualsAndHashCode</h3><p>使用@EqualsAndHashCode注解可以自动生成hashCode和equals方法，默认包含所有类属性，使用@EqualsAndHashCode.Exclude可以排除属性的生成。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Getter</span><br><span class="line">@Setter</span><br><span class="line">@EqualsAndHashCode</span><br><span class="line">public class EqualsAndHashCodeExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    @EqualsAndHashCode.Exclude</span><br><span class="line">    private String name;</span><br><span class="line">    @EqualsAndHashCode.Exclude</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        EqualsAndHashCodeExample example1 = new EqualsAndHashCodeExample();</span><br><span class="line">        example1.setId(1L);</span><br><span class="line">        example1.setName(&quot;test&quot;);</span><br><span class="line">        example1.setAge(20);</span><br><span class="line">        EqualsAndHashCodeExample example2 = new EqualsAndHashCodeExample();</span><br><span class="line">        example2.setId(1L);</span><br><span class="line">        //equals方法只对比id，返回true</span><br><span class="line">        System.out.println(example1.equals(example2));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">public class EqualsAndHashCodeExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public EqualsAndHashCodeExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public boolean equals(final Object o) &#123;</span><br><span class="line">        if (o == this) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125; else if (!(o instanceof EqualsAndHashCodeExample)) &#123;</span><br><span class="line">            return false;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            EqualsAndHashCodeExample other = (EqualsAndHashCodeExample)o;</span><br><span class="line">            if (!other.canEqual(this)) &#123;</span><br><span class="line">                return false;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                Object this$id = this.getId();</span><br><span class="line">                Object other$id = other.getId();</span><br><span class="line">                if (this$id == null) &#123;</span><br><span class="line">                    if (other$id != null) &#123;</span><br><span class="line">                        return false;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; else if (!this$id.equals(other$id)) &#123;</span><br><span class="line">                    return false;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                return true;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected boolean canEqual(final Object other) &#123;</span><br><span class="line">        return other instanceof EqualsAndHashCodeExample;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public int hashCode() &#123;</span><br><span class="line">        int PRIME = true;</span><br><span class="line">        int result = 1;</span><br><span class="line">        Object $id = this.getId();</span><br><span class="line">        int result = result * 59 + ($id == null ? 43 : $id.hashCode());</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="XxConstructor"><a href="#XxConstructor" class="headerlink" title="#@XxConstructor"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#xxconstructor">#</a>@XxConstructor</h3><p>使用@XxConstructor注解可以自动生成构造方法，有@NoArgsConstructor、@RequiredArgsConstructor和@AllArgsConstructor三个注解可以使用。</p>
<ul>
<li>@NoArgsConstructor：生成无参构造函数。</li>
<li>@RequiredArgsConstructor：生成包含必须参数的构造函数，使用@NonNull注解的类属性为必须参数。</li>
<li>@AllArgsConstructor：生成包含所有参数的构造函数。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@NoArgsConstructor</span><br><span class="line">@RequiredArgsConstructor(staticName = &quot;of&quot;)</span><br><span class="line">@AllArgsConstructor</span><br><span class="line">public class ConstructorExample &#123;</span><br><span class="line">    @NonNull</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //无参构造器</span><br><span class="line">        ConstructorExample example1 = new ConstructorExample();</span><br><span class="line">        //全部参数构造器</span><br><span class="line">        ConstructorExample example2 = new ConstructorExample(1L,&quot;test&quot;,20);</span><br><span class="line">        //@NonNull注解的必须参数构造器</span><br><span class="line">        ConstructorExample example3 = ConstructorExample.of(1L);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">public class ConstructorExample &#123;</span><br><span class="line">    @NonNull</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public ConstructorExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private ConstructorExample(@NonNull final Long id) &#123;</span><br><span class="line">        if (id == null) &#123;</span><br><span class="line">            throw new NullPointerException(&quot;id is marked non-null but is null&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.id = id;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static ConstructorExample of(@NonNull final Long id) &#123;</span><br><span class="line">        return new ConstructorExample(id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public ConstructorExample(@NonNull final Long id, final String name, final Integer age) &#123;</span><br><span class="line">        if (id == null) &#123;</span><br><span class="line">            throw new NullPointerException(&quot;id is marked non-null but is null&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.id = id;</span><br><span class="line">            this.name = name;</span><br><span class="line">            this.age = age;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Data"><a href="#Data" class="headerlink" title="#@Data"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#data">#</a>@Data</h3><p>@Data是一个方便使用的组合注解，是@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstructor的组合体。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Data</span><br><span class="line">public class DataExample &#123;</span><br><span class="line">    @NonNull</span><br><span class="line">    private Long id;</span><br><span class="line">    @EqualsAndHashCode.Exclude</span><br><span class="line">    private String name;</span><br><span class="line">    @EqualsAndHashCode.Exclude</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //@RequiredArgsConstructor已生效</span><br><span class="line">        DataExample example1 = new DataExample(1L);</span><br><span class="line">        //@Getter @Setter已生效</span><br><span class="line">        example1.setName(&quot;test&quot;);</span><br><span class="line">        example1.setAge(20);</span><br><span class="line">        //@ToString已生效</span><br><span class="line">        System.out.println(example1);</span><br><span class="line">        DataExample example2 = new DataExample(1L);</span><br><span class="line">        //@EqualsAndHashCode已生效</span><br><span class="line">        System.out.println(example1.equals(example2));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">public class DataExample &#123;</span><br><span class="line">    @NonNull</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public DataExample(@NonNull final Long id) &#123;</span><br><span class="line">        if (id == null) &#123;</span><br><span class="line">            throw new NullPointerException(&quot;id is marked non-null but is null&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.id = id;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @NonNull</span><br><span class="line">    public Long getId() &#123;</span><br><span class="line">        return this.id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return this.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Integer getAge() &#123;</span><br><span class="line">        return this.age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setId(@NonNull final Long id) &#123;</span><br><span class="line">        if (id == null) &#123;</span><br><span class="line">            throw new NullPointerException(&quot;id is marked non-null but is null&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            this.id = id;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setName(final String name) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setAge(final Integer age) &#123;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public boolean equals(final Object o) &#123;</span><br><span class="line">        if (o == this) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125; else if (!(o instanceof DataExample)) &#123;</span><br><span class="line">            return false;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            DataExample other = (DataExample)o;</span><br><span class="line">            if (!other.canEqual(this)) &#123;</span><br><span class="line">                return false;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                Object this$id = this.getId();</span><br><span class="line">                Object other$id = other.getId();</span><br><span class="line">                if (this$id == null) &#123;</span><br><span class="line">                    if (other$id != null) &#123;</span><br><span class="line">                        return false;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; else if (!this$id.equals(other$id)) &#123;</span><br><span class="line">                    return false;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                return true;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected boolean canEqual(final Object other) &#123;</span><br><span class="line">        return other instanceof DataExample;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public int hashCode() &#123;</span><br><span class="line">        int PRIME = true;</span><br><span class="line">        int result = 1;</span><br><span class="line">        Object $id = this.getId();</span><br><span class="line">        int result = result * 59 + ($id == null ? 43 : $id.hashCode());</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String toString() &#123;</span><br><span class="line">        return &quot;DataExample(id=&quot; + this.getId() + &quot;, name=&quot; + this.getName() + &quot;, age=&quot; + this.getAge() + &quot;)&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Value"><a href="#Value" class="headerlink" title="#@Value"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#value">#</a>@Value</h3><p>使用@Value注解可以把类声明为不可变的，声明后此类相当于final类，无法被继承，其属性也会变成final属性。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Value</span><br><span class="line">public class ValueExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //只能使用全参构造器</span><br><span class="line">        ValueExample example = new ValueExample(1L,&quot;test&quot;,20);</span><br><span class="line">        // example.setName(&quot;andy&quot;) //没有生成setter方法，会报错</span><br><span class="line">        // example.name=&quot;andy&quot; //字段被设置为final类型，会报错</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">public final class ValueExample &#123;</span><br><span class="line">    private final Long id;</span><br><span class="line">    private final String name;</span><br><span class="line">    private final Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        new ValueExample(1L, &quot;test&quot;, 20);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public ValueExample(final Long id, final String name, final Integer age) &#123;</span><br><span class="line">        this.id = id;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Long getId() &#123;</span><br><span class="line">        return this.id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return this.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Integer getAge() &#123;</span><br><span class="line">        return this.age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Builder"><a href="#Builder" class="headerlink" title="#@Builder"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#builder">#</a>@Builder</h3><p>使用@Builder注解可以通过建造者模式来创建对象，建造者模式加链式调用，创建对象太方便了！</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Builder</span><br><span class="line">@ToString</span><br><span class="line">public class BuilderExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        BuilderExample example = BuilderExample.builder()</span><br><span class="line">                .id(1L)</span><br><span class="line">                .name(&quot;test&quot;)</span><br><span class="line">                .age(20)</span><br><span class="line">                .build();</span><br><span class="line">        System.out.println(example);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">public class BuilderExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    BuilderExample(final Long id, final String name, final Integer age) &#123;</span><br><span class="line">        this.id = id;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static BuilderExample.BuilderExampleBuilder builder() &#123;</span><br><span class="line">        return new BuilderExample.BuilderExampleBuilder();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String toString() &#123;</span><br><span class="line">        return &quot;BuilderExample(id=&quot; + this.id + &quot;, name=&quot; + this.name + &quot;, age=&quot; + this.age + &quot;)&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static class BuilderExampleBuilder &#123;</span><br><span class="line">        private Long id;</span><br><span class="line">        private String name;</span><br><span class="line">        private Integer age;</span><br><span class="line"></span><br><span class="line">        BuilderExampleBuilder() &#123;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public BuilderExample.BuilderExampleBuilder id(final Long id) &#123;</span><br><span class="line">            this.id = id;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public BuilderExample.BuilderExampleBuilder name(final String name) &#123;</span><br><span class="line">            this.name = name;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public BuilderExample.BuilderExampleBuilder age(final Integer age) &#123;</span><br><span class="line">            this.age = age;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public BuilderExample build() &#123;</span><br><span class="line">            return new BuilderExample(this.id, this.name, this.age);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public String toString() &#123;</span><br><span class="line">            return &quot;BuilderExample.BuilderExampleBuilder(id=&quot; + this.id + &quot;, name=&quot; + this.name + &quot;, age=&quot; + this.age + &quot;)&quot;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="SneakyThrows"><a href="#SneakyThrows" class="headerlink" title="#@SneakyThrows"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#sneakythrows">#</a>@SneakyThrows</h3><p>还在手动捕获并抛出异常？使用@SneakyThrows注解自动实现试试！</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">public class SneakyThrowsExample &#123;</span><br><span class="line"></span><br><span class="line">    //自动抛出异常，无需处理</span><br><span class="line">    @SneakyThrows(UnsupportedEncodingException.class)</span><br><span class="line">    public static byte[] str2byte(String str)&#123;</span><br><span class="line">        return str.getBytes(&quot;UTF-8&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        String str = &quot;Hello World!&quot;;</span><br><span class="line">        System.out.println(str2byte(str).length);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class SneakyThrowsExample &#123;</span><br><span class="line">    public SneakyThrowsExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static byte[] str2byte(String str) &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            return str.getBytes(&quot;UTF-8&quot;);</span><br><span class="line">        &#125; catch (UnsupportedEncodingException var2) &#123;</span><br><span class="line">            throw var2;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Synchronized"><a href="#Synchronized" class="headerlink" title="#@Synchronized"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#synchronized">#</a>@Synchronized</h3><p>当我们在多个线程中访问同一资源时，往往会出现线程安全问题，以前我们往往使用synchronized关键字修饰方法来实现同步访问。使用@Synchronized注解同样可以实现同步访问。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">package com.macro.mall.tiny.example;</span><br><span class="line"></span><br><span class="line">import lombok.*;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Data</span><br><span class="line">public class SynchronizedExample &#123;</span><br><span class="line">    @NonNull</span><br><span class="line">    private Integer count;</span><br><span class="line"></span><br><span class="line">    @Synchronized</span><br><span class="line">    @SneakyThrows</span><br><span class="line">    public void reduceCount(Integer id) &#123;</span><br><span class="line">        if (count &gt; 0) &#123;</span><br><span class="line">            Thread.sleep(500);</span><br><span class="line">            count--;</span><br><span class="line">            System.out.println(String.format(&quot;thread-%d count:%d&quot;, id, count));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //添加@Synchronized三个线程可以同步调用reduceCount方法</span><br><span class="line">        SynchronizedExample example = new SynchronizedExample(20);</span><br><span class="line">        new ReduceThread(1, example).start();</span><br><span class="line">        new ReduceThread(2, example).start();</span><br><span class="line">        new ReduceThread(3, example).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @RequiredArgsConstructor</span><br><span class="line">    static class ReduceThread extends Thread &#123;</span><br><span class="line">        @NonNull</span><br><span class="line">        private Integer id;</span><br><span class="line">        @NonNull</span><br><span class="line">        private SynchronizedExample example;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            while (example.getCount() &gt; 0) &#123;</span><br><span class="line">                example.reduceCount(id);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">public class SynchronizedExample &#123;</span><br><span class="line">    private final Object $lock = new Object[0];</span><br><span class="line">    @NonNull</span><br><span class="line">    private Integer count;</span><br><span class="line"></span><br><span class="line">    public void reduceCount(Integer id) &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            synchronized(this.$lock) &#123;</span><br><span class="line">                if (this.count &gt; 0) &#123;</span><br><span class="line">                    Thread.sleep(500L);</span><br><span class="line">                    Integer var3 = this.count;</span><br><span class="line">                    Integer var4 = this.count = this.count - 1;</span><br><span class="line">                    System.out.println(String.format(&quot;thread-%d count:%d&quot;, id, this.count));</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (Throwable var7) &#123;</span><br><span class="line">            throw var7;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="With"><a href="#With" class="headerlink" title="#@With"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#with">#</a>@With</h3><p>使用@With注解可以实现对原对象进行克隆，并改变其一个属性，使用时需要指定全参构造方法。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@With</span><br><span class="line">@AllArgsConstructor</span><br><span class="line">public class WithExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        WithExample example1 = new WithExample(1L, &quot;test&quot;, 20);</span><br><span class="line">        WithExample example2 = example1.withAge(22);</span><br><span class="line">        //将原对象进行clone并设置age，返回false</span><br><span class="line">        System.out.println(example1.equals(example2));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">public class WithExample &#123;</span><br><span class="line">    private Long id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line"></span><br><span class="line">    public WithExample withId(final Long id) &#123;</span><br><span class="line">        return this.id == id ? this : new WithExample(id, this.name, this.age);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public WithExample withName(final String name) &#123;</span><br><span class="line">        return this.name == name ? this : new WithExample(this.id, name, this.age);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public WithExample withAge(final Integer age) &#123;</span><br><span class="line">        return this.age == age ? this : new WithExample(this.id, this.name, age);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public WithExample(final Long id, final String name, final Integer age) &#123;</span><br><span class="line">        this.id = id;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Getter-lazy-true"><a href="#Getter-lazy-true" class="headerlink" title="#@Getter(lazy&#x3D;true)"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#getter-lazy-true">#</a>@Getter(lazy&#x3D;true)</h3><p>当我们获取某一个属性比较消耗资源时，可以给@Getter添加<code>lazy=true</code>属性实现懒加载，会生成Double Check Lock 样板代码对属性进行懒加载。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">public class GetterLazyExample &#123;</span><br><span class="line">    @Getter(lazy = true)</span><br><span class="line">    private final double[] cached = expensive();</span><br><span class="line"></span><br><span class="line">    private double[] expensive() &#123;</span><br><span class="line">        double[] result = new double[1000000];</span><br><span class="line">        for (int i = 0; i &lt; result.length; i++) &#123;</span><br><span class="line">            result[i] = Math.asin(i);</span><br><span class="line">        &#125;</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //使用Double Check Lock 样板代码对属性进行懒加载</span><br><span class="line">        GetterLazyExample example = new GetterLazyExample();</span><br><span class="line">        System.out.println(example.getCached().length);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">public class GetterLazyExample &#123;</span><br><span class="line">    private final AtomicReference&lt;Object&gt; cached = new AtomicReference();</span><br><span class="line"></span><br><span class="line">    public GetterLazyExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private double[] expensive() &#123;</span><br><span class="line">        double[] result = new double[1000000];</span><br><span class="line"></span><br><span class="line">        for(int i = 0; i &lt; result.length; ++i) &#123;</span><br><span class="line">            result[i] = Math.asin((double)i);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public double[] getCached() &#123;</span><br><span class="line">        Object value = this.cached.get();</span><br><span class="line">        if (value == null) &#123;</span><br><span class="line">            synchronized(this.cached) &#123;</span><br><span class="line">                value = this.cached.get();</span><br><span class="line">                if (value == null) &#123;</span><br><span class="line">                    double[] actualValue = this.expensive();</span><br><span class="line">                    value = actualValue == null ? this.cached : actualValue;</span><br><span class="line">                    this.cached.set(value);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return (double[])((double[])(value == this.cached ? null : value));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Log"><a href="#Log" class="headerlink" title="#@Log"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#log">#</a>@Log</h3><p>使用@Log注解，可以直接生成日志对象log，通过log对象可以直接打印日志。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Log</span><br><span class="line">public class LogExample &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        log.info(&quot;level info&quot;);</span><br><span class="line">        log.warning(&quot;level warning&quot;);</span><br><span class="line">        log.severe(&quot;level severe&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class LogExample &#123;</span><br><span class="line">    private static final Logger log = Logger.getLogger(LogExample.class.getName());</span><br><span class="line"></span><br><span class="line">    public LogExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        log.info(&quot;level info&quot;);</span><br><span class="line">        log.warning(&quot;level warning&quot;);</span><br><span class="line">        log.severe(&quot;level severe&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Slf4j"><a href="#Slf4j" class="headerlink" title="#@Slf4j"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#slf4j">#</a>@Slf4j</h3><p>使用Lombok生成日志对象时，根据使用日志实现的不同，有多种注解可以使用。比如@Log、@Log4j、@Log4j2、@Slf4j等。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Created by macro on 2020/12/17.</span><br><span class="line"> */</span><br><span class="line">@Slf4j</span><br><span class="line">public class LogSlf4jExample &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        log.info(&quot;level:&#123;&#125;&quot;,&quot;info&quot;);</span><br><span class="line">        log.warn(&quot;level:&#123;&#125;&quot;,&quot;warn&quot;);</span><br><span class="line">        log.error(&quot;level:&#123;&#125;&quot;, &quot;error&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译后Lombok会生成如下代码。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class LogSlf4jExample &#123;</span><br><span class="line">    private static final Logger log = LoggerFactory.getLogger(LogSlf4jExample.class);</span><br><span class="line"></span><br><span class="line">    public LogSlf4jExample() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        log.info(&quot;level:&#123;&#125;&quot;, &quot;info&quot;);</span><br><span class="line">        log.warn(&quot;level:&#123;&#125;&quot;, &quot;warn&quot;);</span><br><span class="line">        log.error(&quot;level:&#123;&#125;&quot;, &quot;error&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Lombok原理"><a href="#Lombok原理" class="headerlink" title="#Lombok原理"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#lombok%E5%8E%9F%E7%90%86">#</a>Lombok原理</h2><p>如果IDEA不安装Lombok插件的话，我们打开使用Lombok的项目是无法通过编译的。装了以后IDEA才会提示我们Lombok为我们生成的方法和属性。</p>
<p>使用了@Data注解以后，查看类结构可以发现getter、setter、toString等方法。</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:47:26_lombok_start_02-40bc47fb.png"></p>
<p>打开target目录下的<code>.class</code>文件，我们可以看到Lombok为我们生成的代码，可见Lombok是通过解析注解，然后在编译时生成代码来实现Java代码的功能增强的。</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:47:26_lombok_start_03-af866c02.png"></p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="#参考资料"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99">#</a>参考资料</h2><p>官方文档：<a target="_blank" rel="noopener" href="https://projectlombok.org/features/all">https://projectlombok.org/features/all</a></p>
<h2 id="项目源码地址"><a href="#项目源码地址" class="headerlink" title="#项目源码地址"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/lombok_start.html#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80">#</a>项目源码地址</h2><p><a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-lombok">https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-lombok</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/763a7c049cf8.html">https://ko25891wan.gitlab.io/2024/01/763a7c049cf8.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/81ea40d881c9.html" title="MongoDB快速入门，掌握这些刚刚好！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">MongoDB快速入门，掌握这些刚刚好！</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/2a4c9328c6c6.html" title="Linux防火墙Firewall和Iptables的使用"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Linux防火墙Firewall和Iptables的使用</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Lombok%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">Lombok简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Lombok%E9%9B%86%E6%88%90"><span class="toc-number">2.</span> <span class="toc-text">Lombok集成</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Lombok%E4%BD%BF%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">Lombok使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#val"><span class="toc-number">3.1.</span> <span class="toc-text">val</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#NonNull"><span class="toc-number">3.2.</span> <span class="toc-text">@NonNull</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Cleanup"><span class="toc-number">3.3.</span> <span class="toc-text">@Cleanup</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Getter-Setter"><span class="toc-number">3.4.</span> <span class="toc-text">@Getter&#x2F;@Setter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ToString"><span class="toc-number">3.5.</span> <span class="toc-text">@ToString</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#EqualsAndHashCode"><span class="toc-number">3.6.</span> <span class="toc-text">@EqualsAndHashCode</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#XxConstructor"><span class="toc-number">3.7.</span> <span class="toc-text">@XxConstructor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Data"><span class="toc-number">3.8.</span> <span class="toc-text">@Data</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Value"><span class="toc-number">3.9.</span> <span class="toc-text">@Value</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Builder"><span class="toc-number">3.10.</span> <span class="toc-text">@Builder</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SneakyThrows"><span class="toc-number">3.11.</span> <span class="toc-text">@SneakyThrows</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Synchronized"><span class="toc-number">3.12.</span> <span class="toc-text">@Synchronized</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#With"><span class="toc-number">3.13.</span> <span class="toc-text">@With</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Getter-lazy-true"><span class="toc-number">3.14.</span> <span class="toc-text">@Getter(lazy&#x3D;true)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Log"><span class="toc-number">3.15.</span> <span class="toc-text">@Log</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Slf4j"><span class="toc-number">3.16.</span> <span class="toc-text">@Slf4j</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Lombok%E5%8E%9F%E7%90%86"><span class="toc-number">4.</span> <span class="toc-text">Lombok原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">5.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E5%9C%B0%E5%9D%80"><span class="toc-number">6.</span> <span class="toc-text">项目源码地址</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>